package com.fau.wmbj.server.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.vladmihalcea.hibernate.type.json.JsonStringType;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.TypeDef;

import javax.persistence.*;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 权限菜单
 *
 * @author jiawei
 * @version 1.0
 * @datetime 2023-03-08 17:21
 */
@Getter
@Setter
@Entity(name = "f_menu")
@org.hibernate.annotations.Table(appliesTo = "f_menu", comment = "菜单")
@TypeDef(name = "json", typeClass = JsonStringType.class)
public class Menu extends OrderedEntity<Long> implements Serializable {

    /**
     * 名称
     */
    @Column(name = "name_")
    private String name;

    /**
     * 名称
     */
    @Column(name = "path_")
    private String path;

    /**
     * 名称
     */
    @Column(name = "component_")
    private String component;

    /**
     * title
     */
    @Column(name = "title_")
    private String title;

    /**
     * 权限标识
     */
    @Column(name = "i18n_key")
    private String i18nKey;

    /**
     * 图标 (<a href="https://icones.js.org/">...</a>)
     */
    @Column(name = "icon_")
    private String icon;

    /**
     * 缓存页面
     */
    @Column(name = "keep_alive")
    private Boolean keepAlive;

    /**
     * 是否在菜单中隐藏该路由
     */
    @Column(name = "hide_in_menu")
    private Boolean hideInMenu;

    /**
     * 进入该路由时激活的菜单键,该路由不在菜单中,假设路由是"user_detail"，如果设置为"user_list"，则会激活"用户列表"菜单项
     * 例 activeMenu?: import('@elegant-router/types').RouteKey;
     */
    @Column(name = "active_menu")
    private String activeMenu;

    /**
     * 是否为常量路由，无需登录，并且该路由在前端定义
     */
    @Column(name = "constant")
    private Boolean constant;

    /**
     * 权限标识
     */
    @Column(name = "local_icon")
    private String localIcon;

    /**
     * 路由的外部链接
     */
    @Column(name = "href_")
    private String href;

    /**
     * 默认情况下，相同路径的路由会共享一个标签页，若设置为true，则使用多个标签页
     */
    @Column(name = "multi_tab")
    private String multiTab;

    /**
     * 若设置，路由将在标签页中固定显示，其值表示固定标签页的顺序
     */
    @Column(name = "fixed_index_in_tab")
    private Integer fixedIndexInTab;

    /**
     * 父级
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id")
    private Menu parent;

    /**
     * 子集
     */
    @JsonIgnore
    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @OrderBy(" order asc")
    private Set<Menu> children = new HashSet<>();

    /**
     * 角色
     */
    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "menus")
    private Set<Role> roles = new HashSet<>();

    /**
     * 单位
     */
    @JsonIgnore
    @ManyToMany(fetch = FetchType.LAZY, mappedBy = "menus")
    private Set<Company> companies = new HashSet<>();

    /**
     * 路径
     */
    @Transient
    private Map<String, Object> meta;
}
